import Vue from 'vue'
import ElementUI from 'element-ui'
import VideoPlayer from 'vue-video-player';
import 'element-ui/lib/theme-chalk/index.css';
import 'video.js/dist/video-js.css'
import 'vue-video-player/src/custom-theme.css'
import App from './App'
import router from './router'
import store from './store'
import '@/icons' // icon
import '@/permission' // 权限
import axios from 'axios'
import {
    Message,
    MessageBox
} from 'element-ui'
import {
    getToken
} from '@/utils/auth'
import qs from 'qs';
import GeminiScrollbar from 'vue-gemini-scrollbar' //滚动条优化

Vue.use(GeminiScrollbar)
Vue.use(ElementUI);
Vue.use(VideoPlayer);
let httpL = ''
if (window.location.href.indexOf('https') >= 0) {
    httpL = 'https://'
} else {
    httpL = 'http://'
}
let httpName = httpL + window.location.host;
let BASE_API = 'http://58.33.165.250:9504/API/105/api';
if (httpName.indexOf('localhost') == -1) {
   // BASE_API = httpName + '/API/105/api/'
}
// 创建axios实例
const service = axios.create({
    // baseURL: process.env.BASE_API, // api的base_url
    baseURL: BASE_API, // api的base_url
    timeout: 30000, // 请求超时时间
    headers: { 'content-type': 'application/x-www-form-urlencoded' }
});
//service.defaults.withCredentials = true  //请求开启cookie
// request拦截器
service.interceptors.request.use(config => {
    if (config.method === 'post') {
        config.data = qs.stringify(config.data);
    }
    if (store.getters.token) {
        config.headers['Authorization'] = "ticket " + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
    }
    return config
}, error => {
    console.log(error) // for debug
    Promise.reject(error)
});

// respone拦截器
service.interceptors.response.use(
    response => {
        const res = response.data
        let data = JSON.parse(res);
        if (!data.isSuccess) {
            MessageBox({
                message: data.msg,
            })
            return Promise.reject('error')
        } else {
            return data
        }
    },
    error => {
        console.log('err' + error); // for debug
        Message({
            message: error.message,
            type: 'error',
            showClose: true,
            duration: 5 * 1000
        })
        return Promise.reject(error);
    }
)
Vue.prototype.service = service; //注册全局service

Vue.config.productionTip = false;
new Vue({
    el: '#app',
    router,
    store,
    template: '<App/>',
    components: {
        App
    }
});